window.onerror=function(){
    return true;
};
//var SiteConfig={
//    InfoApiDomain:'http://123.uuola.com/txcms',
//    InfoDomain:'http://info.uuola.com/txcms'
//};
var SiteConfig={
    InfoApiDomain:"http://api.info.uuola.com",
    InfoDomain:"http://wow.uuola.com"
};

function infoHit(){
    $.getScript('http://nap.uuola.com/info/hit/'+Page.infoid);
};

function outInfoList(c,cr,ls,t,h,f){
    var p={
        cid:c,
        cr:cr,
        ls:ls,
        type:t,
        descByHit:h
    };
    var cmdParam ={
        cache:true,
        url:SiteConfig.InfoApiDomain+'/info/outInfoList.shtm',
        dataType:"jsonp",
        jsonp:"jsonpcall",
        data:$.param(p),
        success:function(d){
            f(d);
        }
    };
    $.ajax(cmdParam);
};
/**
 * info page 推荐阅读
 */
function showRecmdReadList(c){
    var tpl='<li><a href="@domain/info/view/@id">· @title</a><span>@time</span></li>';
    var a = [];
    var jqRecmdRead = $('#recmdRead');
    outInfoList(c,2,8,'recmd',0,function(x){
        for(var k in x){
            a.push(tpl
                .replace(/@id/,x[k].infoid)
                .replace(/@title/,x[k].title)
                .replace(/@time/, ut.parseDate(x[k].pubtime, 10))
                .replace(/@domain/,SiteConfig.InfoDomain)
                );
        }
        jqRecmdRead.html(a.join(''));
    });
};

function showHitTopList(c){
    var tpl='<li><a href="@domain/info/view/@id" title="@title">· @tit</a><span>(围观@hit次)</span></li>';
    var a = [];
    var jqhitTop = $('#hitTop');
    outInfoList(c,0,10,'top',1,function(x){
        for(var k in x){
            a.push(tpl
                .replace(/@id/,x[k].infoid)
                .replace(/@title/,x[k].title)
                .replace(/@tit/,ut.cutstr(x[k].title, 14))
                .replace(/@hit/, x[k].hitnum)
                .replace(/@domain/,SiteConfig.InfoDomain)
                );
        }
        jqhitTop.html(a.join(''));
    });
};

function showRollInfoList(ls){
    var tpl='<li><a href="@domain/info/view/@id" title="@title">· @tit</a><span>@time</span></li>';
    var a = [];
    var jqRollInfo = $('#rollInfo');
    outInfoList(0,5,ls||10,'roll',0,function(x){
        for(var k in x){
            a.push(tpl
                .replace(/@id/,x[k].infoid)
                .replace(/@title/,x[k].title)
                .replace(/@tit/,ut.cutstr(x[k].title, 14))
                .replace(/@time/, ut.parseDate(x[k].pubtime, 10))
                .replace(/@domain/,SiteConfig.InfoDomain)
                );
        }
        jqRollInfo.html(a.join(''));
    });
};

function showPicInfoList(c){
    var tpl='<li><a href="@domain/info/view/@id" title="@title" target="_blank"><img src="@mspic"/></a><b>@tit</b></li>';
    var a = [];
    var jqPicInfo = $('#picInfo');
    outInfoList(c,3,10,1,0,function(x){
        for(var k in x){
            a.push(tpl
                .replace(/@id/,x[k].infoid)
                .replace(/@title/,x[k].title)
                .replace(/@tit/,ut.cutstr(x[k].title, 16))
                .replace(/@mspic/, ImgKit.getResizeImgUrl(x[k].pic,'ss'))
                .replace(/@domain/,SiteConfig.InfoDomain)
                );
        }
        jqPicInfo.html(a.join(''));
    });
};

function displayCatInfo(cid){
    var jqInfoListGrid = $('#infoListGrid');
    var jqListMoreBtn = $('#listMore');
    var pgno = parseInt(jqInfoListGrid.data('cpno')) || 1;
    var ls = 15;
    var cr = (ls * ( pgno - 1 ));
    var p={
        cid:cid,
        ls:ls,
        cr:cr
    };
    var cmdParam ={
        cache:true,
        url:SiteConfig.InfoApiDomain+'/info/outCatInfoList.shtm',
        dataType:"jsonp",
        jsonp:"jsonpcall",
        data:$.param(p),
        success:function(d){
            var x = d;
            var rn=x.rownum;
            if(rn>0){
                jqInfoListGrid.data('cpno',pgno+1);
                jqInfoListGrid.append(template.render("listTpl", x));
            }else{
                jqListMoreBtn.html('喔~ 到底了,没过瘾? <a href="'+SiteConfig.InfoDomain+'/site/home.html?rf=info_list_bottom">看看其他的吧。</a>');
                jqListMoreBtn.unbind('click');
            }
        }
    };
    $.ajax(cmdParam);
};

function parseTagsNav(tags){
    var tpl=' <a target="_blank" href="@domain/info/tags.shtm?id=@tagid&na=@tagname" title="查看和标签 ‘@tagname’ 有关的文章">@tagname</a>';
    var a =['| <span>标签：'];
    for(var k in tags){
        a.push(tpl
            .replace(/@tagid/,tags[k].tagid)
            .replace(/@tagname/g,tags[k].tagname)
            .replace(/@domain/,SiteConfig.InfoDomain)
            );
    }
    a.push('</span>');
    if(a.length==2){
        return '';
    }
    return a.join('')
};

//view img fix
function fixViewContentImg(){
    $('img.aimg').aeImageResize({
        width:510
    });
};

// template inject function
if(typeof(template)!="undefined"){
    template.helper('getResizeImgUrl', ImgKit.getResizeImgUrl);
    template.helper('parseDate', ut.parseDate);
    template.helper('infoDomain', SiteConfig.InfoDomain);
    template.helper('parseTagsNav',parseTagsNav);
}
 
// weibo topic live
function weiboTopic(eid,t){
    var topic = ut.enc(t||'噢啦网');
    var ifr = '<iframe width=\"100%\" height=\"600\" frameborder=\"0\" scrolling=\"no\" src=\"http://widget.weibo.com/livestream/listlive.php?language=zh_cn&width=0&height=600&uid=3221011785&skin=1&refer=1&appkey=4098733415&pic=1&titlebar=1&border=1&publish=1&atalk=1&recomm=0&at=0&atopic='+topic+'&ptopic='+topic+'&colordiy=0&dpc=1\"></iframe>';
    $('#'+eid).html(ifr);
};

function weiboShow(eid){
    var ifr ='<iframe width="100%" height="600" class="share_self"  frameborder="0" scrolling="no" src="http://widget.weibo.com/weiboshow/index.php?language=&width=0&height=600&fansRow=2&ptype=1&speed=100&skin=1&isTitle=0&noborder=1&isWeibo=1&isFans=0&uid=3221011785&verifier=1f2c1184&dpc=1"></iframe>';
    $('#'+eid).html(ifr);
};

function shareWeibo(id) {
    var el=jQuery("#snshare"+id);
    window.open("http://service.weibo.com/share/share.php?appkey=&ralateUid=3221011785&url=" + ut.enc(el.data('url')) + "&title=" + ut.enc(el.data('title')+'...'+el.data('summary')) + "&pic=" + el.data('pic'), "_blank", "width=650,height=550");
};
function shareQQwb(id) {
    var el=jQuery("#snshare"+id);
    window.open("http://share.v.t.qq.com/index.php?c=share&a=index&appkey=801302287&url=" + ut.enc(el.data('url')) + "&title=" + ut.enc(el.data('title')+'...'+el.data('summary'))+ "&pic=" + el.data('pic') , "_blank", "width=650,height=550");
};
function shareQzone(id){
    var el=jQuery("#snshare"+id);
    window.open('http://sns.qzone.qq.com/cgi-bin/qzshare/cgi_qzshare_onekey?url='+ut.enc(el.data('url'))+'&title='+ut.enc(el.data('title'))+'&pics='+el.data('pic')+'&summary='+ut.enc(el.data('summary')), "_blank", "width=650,height=550");
};
function shareRenren(id){
    var el = jQuery('#snshare'+id);
    window.open('http://widget.renren.com/dialog/share?resourceUrl='+ut.enc(el.data('url'))+'&title='+ut.enc(el.data('title'))+'&pic='+el.data('pic')+'&description='+ut.enc(el.data('summary')), "_blank", "width=650,height=700");
};
function shareKx001(id){
    var el = jQuery('#snshare'+id);
    window.open('http://www.kaixin001.com/rest/records.php?content='+ut.enc(el.data('title')+'...'+el.data('summary'))+'&url='+ut.enc(el.data('url'))+'&starid=0&aid=0&style=11&stime=&sig=&pic='+el.data('pic'), "_blank", "width=650,height=550");
};
function shareMail(id){
    var el = jQuery('#snshare'+id);
    window.open('http://s.jiathis.com/?webid=email&url='+ut.enc(el.data('url'))+'&title='+ut.enc(el.data('title'))+'&uid=89627&jtss=1&pic='+el.data('pic')+'&summary='+el.data('summary'), "_blank");
};
function initShare(se){
    var s =("<a href=\"javascript:shareWeibo(\'@id\');\" class=\"snwb\" title=\"分享到新浪微博\"></a> ");
    s+=("<a href=\"javascript:shareQQwb(\'@id\');\" class=\"qqwb\" title=\"分享到QQ微博\"></a> ");
    s+=("<a href=\"javascript:shareQzone(\'@id\');\" class=\"qzon\" title=\"分享到QQ空间\"></a> ");
    s+=("<a href=\"javascript:shareRenren(\'@id\');\" class=\"renr\" title=\"分享到人人网\"></a> ");
    s+=("<a href=\"javascript:shareKx001(\'@id\');\" class=\"kx001\" title=\"分享到开心网\"></a> ");
    s+=("<a href=\"javascript:shareMail(\'@id\');\" class=\"mail\" title=\"收藏到邮箱\"></a>");
    $(se).each(function(i){
        var snp = $(this);
        snp.html(s.replace(/@id/g,snp.data('id')));
    });
};    
// info break page
function gotoInfoTxt(p,r){
    ut.gopage(SiteConfig.InfoDomain+'/info/view/'+Page.infoid+'?p='+p);
};
function infoBreakPage(){
    var p = ut.get('p')||1;
    var breakMark ='##pagebreak##';
    var pno = va.tint(p)?parseInt(p):1;
    var jqInfoTxt = $('#infotxt');
    var jqOutTxt = $('#outTxt');
    var txt = jqInfoTxt.html().replace(/<hr(.{1,8})pagebreak(.{0,3})>/gi, breakMark);
    if(txt.indexOf(breakMark)<0){
        jqOutTxt.hide();
        jqInfoTxt.slideDown();
        return;
    }
    var txtArray = txt.split(breakMark);
    var rn = txtArray.length;
    jqOutTxt.html(txtArray[pno-1]);
    ut.outpage(pno, rn, 'gotoInfoTxt','infopage', 1, 7);
};

// tag info list
var TagInfo={
    listBox:$('#infoListGrid'),
    loader:$('#loader'),
    curPageTxt:$('#curpage'),
    prevBtn:$('#prevBtn'),
    nextBtn:$('#nextBtn'),
    Url:SiteConfig.InfoApiDomain+'/info/outTagInfoList.shtm',
    init:function(){
        this.loadData(1);
        TagInfo.prevBtn.click(function(){
            TagInfo.loadData(TagInfo.prevBtn.data('pgno'));
        });
        TagInfo.nextBtn.click(function(){
            TagInfo.loadData(TagInfo.nextBtn.data('pgno'));
        });
    },
    loadData:function(pno){
        var gotopage = pno||1;
        var ls = 40;
        var cr = (ls * ( gotopage - 1 ));
        var p={
            tagid:Page.tagid,
            ls:ls,
            cr:cr
        };
        $.ajax({
            cache:true,
            url: TagInfo.Url,
            dataType:"jsonp",
            jsonp:"jsonpcall",
            data:$.param(p),
            beforeSend:function(){
                TagInfo.loader.show();
            },
            success:function(data){
                var x = data;
                var rn=x.rownum;
                var dom = '';
                if(rn>0){
                    dom = template.render("listTpl", x);
                    TagInfo.curPageTxt.text(gotopage);
                    TagInfo.listBox.html(dom);
                    TagInfo.listBox.imagesLoaded(function(){
                        TagInfo.listBox.masonry({
                            itemSelector: '.box'
                        });
                    });
                }
                if(gotopage>1){
                    TagInfo.prevBtn.show().data('pgno',gotopage-1);
                }else{
                    TagInfo.prevBtn.hide().data('pgno','');
                }
                if(rn==ls){
                    TagInfo.nextBtn.show().data('pgno',gotopage+1);
                }else{
                    TagInfo.nextBtn.hide().data('pgno','');
                }
            },
            complete:function(){
                TagInfo.loader.hide();
            }
        });
    }
};